package managedBean.TX.Rep;

import session.TX.*;
import entity.Area;
import entity.PerformanceItem;
import entity.Showing;
import entity.StakeholderPerformance;
import entity.Venue;
import exception.ExistException;
import exception.OccupiedException;
import exception.OutstandingEventsException;
import exception.TimingException;
import exception.VenueConflictException;
import Helper.PerformanceHelper;
import Helper.LayoutHelper;
import Helper.Seats;
import Helper.ShowHelper;
import Helper.columnHelper;
import Helper.rowHelper;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import session.CM.PortalManagementSB;
import session.TX.PerformanceTicketingSBRemote;

/**
 * @Program written by T12 of IS3102/AY2011-12
 */
@ManagedBean
@SessionScoped
public class StakeholderPerformanceMB {
    @EJB
    private PerformanceTicketingSBRemote etbr;
    
    // stakeholder info
    private Long stakeholderId;
    private String stakeholderName;
    // for venues
    private String venueName;
    private String venueAddress;
    private String venueLayout;
    private Integer venueCapacity;
    private Double venueLatitude;
    private Double venueLongtitude;
    private Venue venue;
    private String venueStatus;
    // for events
    private String eventName;
    private String posterPath;
    private Long eventId;
    private String eventDescription;
    private Integer eventDuration;
    private Date eventStartDate;
    private Date eventEndDate;
    private String eventLanguage;
    private String eventRating;
    private String eventGenre;
    private String eventType;
    // shows
    private String selectedEvent;
    private Long selectedEventId;
    private Long selectedShowId;
    private Date selectedDate;
    private String selectedStartDate;
    private String selectedEndDate;
    private Integer selectedHour;
    private Integer selectedMinute;
    private String selectedVenue;
    private String selectedCurrentDate;
    private String showTiming;
    private String selectedShowLayout;
    // others
    private String statusMessage;
    private StakeholderPerformance stakeholder;
    private PerformanceHelper eventHelper;
    private ShowHelper showHelper;
    // for a venue layout
    private Integer layoutRows;
    private Integer layoutColumns;
    // for a specific area layout
    private Integer areaRows;
    private Integer areaColumns;
    private String areaName;
    private String cost;
    private String areaId;
    private Double areaCost;
    // for adding area into the database
    private String [][] areaSeatId;
    private String [][] areaSeatRow2;
    private Integer [][] areaSeatColumn2;
    private boolean [][] areaSeatOccupied2;
    private boolean [][] areaSeatEmpty2;
    private String areaSeatRow;
    private String areaSeatColumn;
    private String areaSeatOccupied;
    private String areaSeatEmpty;
    // setting date ranges
    private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    private SimpleDateFormat dateDayFormat = new SimpleDateFormat("dd/MM/yyyy(EEEE) K:mm aa");
    private String minDate;
    private String maxDate;
    
    @EJB
    PortalManagementSB portalManageSB;
    
    /*
     * Creates a new instance of StakeholderPerformanceMB 
     */
    public StakeholderPerformanceMB() {
    }
    
    /*
     * Accessors and Mutators
     */
    public String getEventDescription() {
        return eventDescription;
    }

    public void setEventDescription(String eventDescription) {
        this.eventDescription = eventDescription;
    }

    public Long getEventId() {
        return eventId;
    }

    public void setEventId(Long eventId) {
        this.eventId = eventId;
    }
    
    public Integer getEventDuration() {
        return eventDuration;
    }

    public void setEventDuration(Integer eventDuration) {
        this.eventDuration = eventDuration;
    }

    public Date getEventEndDate() {
        return eventEndDate;
    }

    public void setEventEndDate(Date eventEndDate) {
        this.eventEndDate = eventEndDate;
    }

    public String getEventGenre() {
        return eventGenre;
    }

    public void setEventGenre(String eventGenre) {
        this.eventGenre = eventGenre;
    }

    public String getEventLanguage() {
        return eventLanguage;
    }

    public void setEventLanguage(String eventLanguage) {
        this.eventLanguage = eventLanguage;
    }

    public String getEventName() {
        return eventName;
    }

    public void setEventName(String eventName) {
        this.eventName = eventName;
    }

    public String getEventRating() {
        return eventRating;
    }

    public void setEventRating(String eventRating) {
        this.eventRating = eventRating;
    }

    public Date getEventStartDate() {
        return eventStartDate;
    }

    public void setEventStartDate(Date eventStartDate) {
        this.eventStartDate = eventStartDate;
    }

    public Integer getAreaColumns() {
        return areaColumns;
    }

    public void setAreaColumns(Integer areaColumns) {
        this.areaColumns = areaColumns;
    }

    public String getAreaName() {
        return areaName;
    }

    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    public Integer getAreaRows() {
        return areaRows;
    }

    public void setAreaRows(Integer areaRows) {
        this.areaRows = areaRows;
    }

    public String getCost() {
        return cost;
    }

    public void setCost(String cost) {
        this.cost = cost;
    }

    public Integer getLayoutColumns() {
        return layoutColumns;
    }

    public void setLayoutColumns(Integer layoutColumns) {
        this.layoutColumns = layoutColumns;
    }

    public Integer getLayoutRows() {
        return layoutRows;
    }

    public void setLayoutRows(Integer layoutRows) {
        this.layoutRows = layoutRows;
    }

    public String getPosterPath() {
        return posterPath;
    }

    public void setPosterPath(String posterPath) {
        this.posterPath = posterPath;
    }

    public String getShowTiming() {
        return showTiming;
    }

    public void setShowTiming(String showTiming) {
        this.showTiming = showTiming;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public StakeholderPerformance getStakeholder() {
        return stakeholder;
    }

    public void setStakeholder(StakeholderPerformance stakeholder) {
        this.stakeholder = stakeholder;
    }

    public String getVenueAddress() {
        return venueAddress;
    }

    public void setVenueAddress(String venueAddress) {
        this.venueAddress = venueAddress;
    }

    public Integer getVenueCapacity() {
        return venueCapacity;
    }

    public void setVenueCapacity(Integer venueCapacity) {
        this.venueCapacity = venueCapacity;
    }

    public Double getVenueLatitude() {
        return venueLatitude;
    }

    public void setVenueLatitude(Double venueLatitude) {
        this.venueLatitude = venueLatitude;
    }

    public String getVenueLayout() {
        return venueLayout;
    }

    public void setVenueLayout(String venueLayout) {
        this.venueLayout = venueLayout;
    }

    public Double getVenueLongtitude() {
        return venueLongtitude;
    }

    public void setVenueLongtitude(Double venueLongtitude) {
        this.venueLongtitude = venueLongtitude;
    }

    public String getVenueName() {
        return venueName;
    }

    public void setVenueName(String venueName) {
        this.venueName = venueName;
    }

    public String getMaxDate() {
        return maxDate;
    }

    public void setMaxDate(String maxDate) {
        this.maxDate = maxDate;
    }

    public String getMinDate() {
        return minDate;
    }

    public void setMinDate(String minDate) {
        this.minDate = minDate;
    }

    public String getStakeholderName() {
        return stakeholderName;
    }

    public void setStakeholderName(String stakeholderName) {
        this.stakeholderName = stakeholderName;
    }

    public String getEventType() {
        return eventType;
    }

    public void setEventType(String eventType) {
        this.eventType = eventType;
    }

    public String getSelectedEvent() {
        return selectedEvent;
    }

    public void setSelectedEvent(String selectedEvent) {
        this.selectedEvent = selectedEvent;
    }

    public Long getSelectedEventId() {
        return selectedEventId;
    }

    public void setSelectedEventId(Long selectedEventId) {
        this.selectedEventId = selectedEventId;
    }

    public Date getSelectedDate() {
        return selectedDate;
    }

    public void setSelectedDate(Date selectedDate) {
        this.selectedDate = selectedDate;
    }

    public Integer getSelectedHour() {
        return selectedHour;
    }

    public void setSelectedHour(Integer selectedHour) {
        this.selectedHour = selectedHour;
    }

    public Integer getSelectedMinute() {
        return selectedMinute;
    }

    public void setSelectedMinute(Integer selectedMinute) {
        this.selectedMinute = selectedMinute;
    }

    public String getSelectedVenue() {
        return selectedVenue;
    }

    public void setSelectedVenue(String selectedVenue) {
        this.selectedVenue = selectedVenue;
    }

    public String getSelectedEndDate() {
        return selectedEndDate;
    }

    public void setSelectedEndDate(String selectedEndDate) {
        this.selectedEndDate = selectedEndDate;
    }

    public String getSelectedStartDate() {
        return selectedStartDate;
    }

    public void setSelectedStartDate(String selectedStartDate) {
        this.selectedStartDate = selectedStartDate;
    }

    public Long getSelectedShowId() {
        return selectedShowId;
    }

    public void setSelectedShowId(Long selectedShowId) {
        this.selectedShowId = selectedShowId;
    }

    public PerformanceHelper getEventHelper() {
        return eventHelper;
    }

    public void setEventHelper(PerformanceHelper eventHelper) {
        this.eventHelper = eventHelper;
    }

    public ShowHelper getShowHelper() {
        return showHelper;
    }

    public void setShowHelper(ShowHelper showHelper) {
        this.showHelper = showHelper;
    }

    public String getSelectedCurrentDate() {
        return selectedCurrentDate;
    }

    public void setSelectedCurrentDate(String selectedCurrentDate) {
        this.selectedCurrentDate = selectedCurrentDate;
    }

    public Venue getVenue() {
        return venue;
    }

    public void setVenue(Venue venue) {
        this.venue = venue;
    }

    public Long getStakeholderId() {
        return stakeholderId;
    }

    public void setStakeholderId(Long stakeholderId) {
        this.stakeholderId = stakeholderId;
    }

    public Integer[][] getAreaSeatColumn2() {
        return areaSeatColumn2;
    }

    public void setAreaSeatColumn2(Integer[][] areaSeatColumn2) {
        this.areaSeatColumn2 = areaSeatColumn2;
    }

    public boolean[][] getAreaSeatEmpty2() {
        return areaSeatEmpty2;
    }

    public void setAreaSeatEmpty(boolean[][] areaSeatEmpty2) {
        this.areaSeatEmpty2 = areaSeatEmpty2;
    }

    public String[][] getAreaSeatId() {
        return areaSeatId;
    }

    public void setAreaSeatId(String[][] areaSeatId) {
        this.areaSeatId = areaSeatId;
    }

    public boolean[][] getAreaSeatOccupied2() {
        return areaSeatOccupied2;
    }

    public void setAreaSeatOccupied(boolean[][] areaSeatOccupied2) {
        this.areaSeatOccupied2 = areaSeatOccupied2;
    }

    public String[][] getAreaSeatRow2() {
        return areaSeatRow2;
    }

    public void setAreaSeatRow(String[][] areaSeatRow2) {
        this.areaSeatRow2 = areaSeatRow2;
    }

    public String getSelectedShowLayout() {
        return selectedShowLayout;
    }

    public void setSelectedShowLayout(String selectedShowLayout) {
        this.selectedShowLayout = selectedShowLayout;
    }

    public String getVenueStatus() {
        return venueStatus;
    }

    public void setVenueStatus(String venueStatus) {
        this.venueStatus = venueStatus;
    }

    public String getAreaSeatColumn() {
        return areaSeatColumn;
    }

    public void setAreaSeatColumn(String areaSeatColumn) {
        this.areaSeatColumn = areaSeatColumn;
    }

    public String getAreaSeatEmpty() {
        return areaSeatEmpty;
    }

    public void setAreaSeatEmpty(String areaSeatEmpty) {
        this.areaSeatEmpty = areaSeatEmpty;
    }

    public String getAreaSeatOccupied() {
        return areaSeatOccupied;
    }

    public void setAreaSeatOccupied(String areaSeatOccupied) {
        this.areaSeatOccupied = areaSeatOccupied;
    }

    public String getAreaSeatRow() {
        return areaSeatRow;
    }

    public void setAreaSeatRow(String areaSeatRow) {
        this.areaSeatRow = areaSeatRow;
    }

    public Double getAreaCost() {
        return areaCost;
    }

    public void setAreaCost(Double areaCost) {
        this.areaCost = areaCost;
    }

    public String getAreaId() {
        return areaId;
    }

    public void setAreaId(String areaId) {
        this.areaId = areaId;
    }
    
    /*
     **************************
     * Methods used in jsf
     **************************
     */
    // will be removed
    public List<String> getStakeholdersName(){
        return etbr.getStakeholdersName();
    }
    
    public Long findId(String stakeholderName){
        return etbr.getStakeholderId(stakeholderName);
    }
    
    /* 
     * For TCube
     */
    //For creating venues // completed and passed
    public void createVenue(ActionEvent event){
        String venueName = String.valueOf(getVenueName());
        String venueAddress = String.valueOf(getVenueAddress());
        String venueLayout = String.valueOf(getVenueLayout());
        Integer venueCapacity = Integer.valueOf(getVenueCapacity());
        Double venueLatitude = Double.valueOf(getVenueLatitude());
        Double venueLongtitude = Double.valueOf(getVenueLongtitude());
        String venueStatus = String.valueOf(this.getVenueStatus());
        boolean active = true;
        if(venueStatus.equalsIgnoreCase("Inactive")){
            active = false;
        }
        try{
            etbr.addVenue(venueName, venueAddress, venueLayout, venueCapacity, venueLatitude, venueLongtitude, active);
            setStatusMessage(venueName + " successfully added.");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        }catch(ExistException ex){
            setStatusMessage(ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Result: " + getStatusMessage(), ""));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    //For removing venues // completed and passed
    public void deleteVenue(Venue venue){
        String venueName = venue.getName();
        //System.out.println("Event Venue: " + venueName);
        try{
            etbr.removeVenue(venueName);
            setStatusMessage("Venue " + venueName + " successfully deleted.");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        }catch(ExistException ex){
            setStatusMessage(ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: " + getStatusMessage(), ""));
        }catch(OutstandingEventsException oe){
            setStatusMessage(oe.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: " + getStatusMessage(), ""));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    //Modify the venue // completed but not tested
    public void modifyVenue(ActionEvent event){
        String venueName = String.valueOf(getVenueName());
        String venueAddress = String.valueOf(getVenueAddress());
        String venueLayout = String.valueOf(getVenueLayout());
        Integer venueCapacity = Integer.valueOf(getVenueCapacity());
        Double venueLatitude = Double.valueOf(getVenueLatitude());
        Double venueLongtitude = Double.valueOf(getVenueLongtitude());
        String venueStatus = String.valueOf(this.getVenueStatus());
        boolean active = true;
        if(venueStatus.equalsIgnoreCase("Inactive")){
            active = false;
        }
        try{
            etbr.modifyVenue(venueName, venueAddress, venueLayout, venueCapacity, venueLatitude, venueLongtitude, active);
            setStatusMessage(venueName + " successfully changed.");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        }catch(ExistException ex){
            setStatusMessage(ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Result: " + getStatusMessage(), ""));
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    //Get all venues // completed and passed
    public List<Venue> getAllVenues(){
        return etbr.viewVenues();
    }
    
    // adds a layout
    public void addLayout(){
        
    }
    
    // removes a layout
    public void removeLayout(){
        
    }
    
    // changes a layout
    public void changeLayout(){
        
    }
    
    // view all layouts
    public void viewLayout(){
        
    }
    
    /*
     * For Stakeholders
     */
    //Creates event // incomplete
    public void createEvent(ActionEvent event){
        String stakeholderName = this.stakeholderName;
        Long stakeholderId = this.stakeholderId;
        /*System.out.println("Create Event: " + stakeholderName);
        System.out.println("Create Event: " + stakeholderId);*/
        String eventName = String.valueOf(getEventName());
        String eventDescription = String.valueOf(getEventDescription());
        Integer eventDuration = Integer.valueOf(getEventDuration());
        String posterPath = String.valueOf(getPosterPath());
        String eventLanguage = String.valueOf(getEventLanguage());
        String eventType = String.valueOf(getEventType());
        String eventRating = String.valueOf(getEventRating());
        String eventGenre = String.valueOf(getEventGenre());
        Date eventStartDate = getEventStartDate();
        Date eventEndDate = getEventEndDate();
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        start.setTime(eventStartDate);
        end.setTime(eventEndDate);
        end.add(Calendar.HOUR, 24);
        end.add(Calendar.MILLISECOND, -1);
        try{
            etbr.addEvent(stakeholderId, eventName, posterPath, eventDescription, eventRating, eventLanguage, eventDuration, eventType, eventGenre, start, end);
            setStatusMessage(eventName + " created successfully.");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        }catch(TimingException t){
            setStatusMessage(t.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Result: " + getStatusMessage(), ""));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    //Removes events // incomplete
    public void deleteEvents(Long eventId){
        String stakeholder = getStakeholderName();
        Long stakeholderId = getStakeholderId();
        try{
            etbr.removeEvent(stakeholderId, eventId);
            setStatusMessage("Event #" + eventId + " removed successfully from " + stakeholderName + ".");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        }catch(OutstandingEventsException oe){
            setStatusMessage(oe.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Result: " + getStatusMessage(), ""));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    //Modifies events // incomplete
    public void modifyEvent(ActionEvent event){
        Long stakeholderId = this.stakeholderId;
        Long eventId = getEventId();
        String eventName = String.valueOf(getEventName());
        String eventDescription = String.valueOf(getEventDescription());
        Integer eventDuration = Integer.valueOf(getEventDuration());
        String posterPath = String.valueOf(getPosterPath());
        String eventLanguage = String.valueOf(getEventLanguage());
        String eventType = String.valueOf(getEventType());
        String eventRating = String.valueOf(getEventRating());
        String eventGenre = String.valueOf(getEventGenre());
        Date eventStartDate = getEventStartDate();
        Date eventEndDate = getEventEndDate();
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        start.setTime(eventStartDate);
        end.setTime(eventEndDate);
        end.add(Calendar.HOUR, 24);
        end.add(Calendar.MILLISECOND, -1);
        try{
            etbr.modifyEvent(stakeholderId, eventId, eventName, posterPath, eventDescription, eventRating, eventLanguage, eventDuration, eventType, eventGenre, start, end);
            setStatusMessage(eventName + " changed successfully.");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        }catch(TimingException t){
            setStatusMessage(t.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Result: " + getStatusMessage(), ""));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public String getResult(){
        String output = (String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("A");
        //System.out.println(output);
        return output;
    }
    
    //Get All Events // incomplete
    public List<PerformanceHelper> getAllEvents(){
        List<PerformanceHelper> output = new ArrayList<PerformanceHelper>();
        Long stakeholderId = getStakeholderId();
        String stakeholderName = getStakeholderName();
        List<PerformanceItem> temp = new ArrayList<PerformanceItem>();
        temp = etbr.getEvent(stakeholderId);
        for(PerformanceItem e: temp){
            PerformanceHelper eh = new PerformanceHelper();
            eh.setId(e.getId());
            eh.setEventName(e.getName());
            eh.setDescription(e.getDescription());
            eh.setDuration(e.getEventDuration());
            eh.setGenre(e.getEventGenre());
            eh.setType(e.getEventType());
            eh.setPoster(e.getEventPoster());
            eh.setRating(e.getEventRating());
            eh.setLanguage(e.getEventLanguage());
            eh.setStartDate(dateFormat.format(e.getStartDate().getTime()));
            eh.setEndDate(dateFormat.format(e.getEndDate().getTime()));
            List<ShowHelper> shows = new ArrayList<ShowHelper>();
            for(Showing s: e.getShow()){
                ShowHelper sh = new ShowHelper();
                sh.setId(s.getId());
                sh.setTiming(dateFormat.format(s.getShowDate().getTime()));
                sh.setVenue(s.getVenue().getName());
                sh.setShowlayout(s.getShowLayout());
                shows.add(sh);
            }
            eh.setShowings(shows);
            output.add(eh);
        }
        return output;
    }
    
    //Adds show // complete
    public void addShow(ActionEvent event){
        Long eventId = Long.valueOf(getSelectedEventId());
        String venue = String.valueOf(getSelectedVenue());
        //System.out.println(venue);
        Integer hours = Integer.valueOf(getSelectedHour());
        Integer minutes = Integer.valueOf(getSelectedMinute());
        Calendar showdate = Calendar.getInstance();
        showdate.setTime(getSelectedDate());
        String layout = String.valueOf(getSelectedShowLayout());
        showdate.add(Calendar.MINUTE, (hours * 60 + minutes));
        try{
            etbr.addShow(eventId, showdate, venue, layout);
            setStatusMessage("Show is successfully added for Event " + getSelectedEvent() + " at " + venue + "(" + dateFormat.format(showdate.getTime()) + ").");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        }catch(VenueConflictException v){
            setStatusMessage(v.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: " + getStatusMessage(), ""));
        }catch(TimingException t){
            setStatusMessage(t.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: " + getStatusMessage(), ""));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    //Removes show // incomplete
    public void cancelShow(Long eventId, Long showId){
        String eventName = etbr.getEventName(eventId);
        try{
            etbr.removeShow(showId, eventId);
            setStatusMessage("Show ID #" + showId + " is deleted successfully from Event ID #" + eventId + "(" + eventName + ").");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        }catch(ExistException e){
            setStatusMessage(e.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: " + getStatusMessage(), ""));
        }catch(OccupiedException o){
            setStatusMessage(o.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: " + getStatusMessage(), ""));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    //Modifies show // incomplete
    public void changeShow(ActionEvent event){
        //System.out.println("Entered changeshow...");
        Long showId = Long.valueOf(getSelectedShowId());
        String venue = String.valueOf(getSelectedVenue());
        Integer hours = Integer.valueOf(getSelectedHour());
        Integer minutes = Integer.valueOf(getSelectedMinute());
        Calendar showdate = Calendar.getInstance();
        showdate.setTime(getSelectedDate());
        showdate.add(Calendar.MINUTE, (hours * 60 + minutes));
        String layout = String.valueOf(getSelectedShowLayout());
        try{
            etbr.modifyShow(showId, venue, layout, showdate);
            setStatusMessage("Show of ID " + showId + " successfully changed to " + dateFormat.format(showdate.getTime()) + " at " + venue + ".");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        }catch(ExistException e){
            setStatusMessage(e.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: " + getStatusMessage(), ""));
        }catch(VenueConflictException v){
            setStatusMessage(v.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: " + getStatusMessage(), ""));
        }
    }
    
    // gets all shows from an event
    public List<ShowHelper> getAllShow(){
        List<ShowHelper> output = new ArrayList<ShowHelper>();
        Long eventId = getSelectedEventId();
        //System.out.println("id: " + eventId);
        for(PerformanceHelper eh: getAllEvents()){
            /*System.out.println("ehid: " + eh.getId());
            System.out.println(eh.getId() +" = " + eventId + ": " + (eh.getId().compareTo(eventId)));*/
            if(eh.getId().compareTo(eventId) == 0){
                output = eh.getShowings();
            }
        }
        // System.out.println("size: " + output.size());
        return output;
    }
    
    //Adds poster // incomplete
    public void removePoster(Long eventid){
        try{
            etbr.removePoster(eventid);
            setStatusMessage("Poster successfully removed from event.");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        }catch(ExistException e){
            setStatusMessage(e.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: " + getStatusMessage(), ""));
        }
    }
    
    //Removes poster // incomplete
    public void removeVenueLayout(String venue){
        try{
            etbr.removeVenueLayout(venueName);
            setStatusMessage("Layout removed from venue " + venue + ".");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        }catch(ExistException e){
            setStatusMessage(e.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: " + getStatusMessage(), ""));
        }
    }
    
    //Changes the poster // incomplete
    public void removeShowLayout(Long showid){
        try{
            etbr.removeShowLayout(showid);
            setStatusMessage("Layout successfully removed from show.");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        }catch(ExistException e){
            setStatusMessage(e.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: " + getStatusMessage(), ""));
        }
    }
    
    //Sets the status of adding area
    public void addAreaStatus(String success){
        System.out.println(success);
        if(success.contains("success:")){
            setStatusMessage(success);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, getStatusMessage(), ""));
        }else{
            setStatusMessage(success);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, getStatusMessage(), ""));
        }
    }
    
    //Removes area // incomplete
    public void removeArea(Long areaid){
        try{
            etbr.removeArea(areaid);
            setStatusMessage("Area #" + areaid + " Successfully Removed");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, getStatusMessage(), ""));
        }catch(ExistException e){
            setStatusMessage(e.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, getStatusMessage(), ""));
        }catch(OutstandingEventsException oe){
            setStatusMessage(oe.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, getStatusMessage(), ""));
        }
    }
    
    //Change the stats of an area // incomplete
    public void modifyArea(){
       
    }
    
    // getting the minimum date
    public String findingMinDate(){
        Calendar temp = Calendar.getInstance();
        Date dateTemp = temp.getTime();
        minDate = dateFormat.format(dateTemp);
        return minDate;
    }
    
    // getting the maximum date
    public String findingMaxDate(){
        Calendar temp = Calendar.getInstance();
        temp.add(Calendar.YEAR, 1);
        Date dateTemp = temp.getTime();
        maxDate = dateFormat.format(dateTemp);
        return maxDate;
    }
    
    public void updateSelectedShowFields(){
        this.selectedShowId = showHelper.getId();
        this.selectedVenue = showHelper.getVenue();
        Calendar temp = Calendar.getInstance();
        temp = etbr.getTiming(selectedShowId);
        this.selectedDate = temp.getTime();
        this.selectedHour = temp.get(Calendar.HOUR_OF_DAY);
        this.selectedMinute = temp.get(Calendar.MINUTE);
        this.showTiming = dateDayFormat.format(temp.getTime());
        this.selectedShowLayout = showHelper.getShowlayout();
    }
    
    public void updateSelectedEventFields(){
        this.eventId = eventHelper.getId();
        this.eventName = eventHelper.getEventName();
        this.posterPath = eventHelper.getPoster();
        this.eventDescription = eventHelper.getDescription();
        this.eventGenre = eventHelper.getGenre();
        this.eventRating = eventHelper.getRating();
        this.eventLanguage = eventHelper.getLanguage();
        this.eventDuration = eventHelper.getDuration();
        this.eventType = eventHelper.getType();
        Calendar start = Calendar.getInstance();
        start = etbr.getEventStartDate(eventId);
        this.eventStartDate = start.getTime();
        Calendar end = Calendar.getInstance();
        end = etbr.getEventEndDate(eventId);
        this.eventEndDate = end.getTime();
        //System.out.println("Start: " + dateFormat.format(eventStartDate));
        //System.out.println("End: " + dateFormat.format(eventEndDate));
    }
    
    public void updateSelectedVenueFields(){
        this.venueName = venue.getName();
        this.venueAddress = venue.getAddress();
        this.venueCapacity = venue.getCapacityCap();
        this.venueLayout = venue.getLayout();
        this.venueLongtitude = venue.getLongitude();
        this.venueLatitude = venue.getLatitude();
    }
    
    public List<Area> getAllAreas(){
        List<Area> output = new ArrayList<Area>();
        Long showId = getSelectedShowId();
        //System.out.println("ShowID: " + showId);
        output = etbr.getAllAreas(showId);
        return output;
    }
    
    public List<LayoutHelper> getColumns(){
        List<LayoutHelper> output = new ArrayList<LayoutHelper>();
        for (int j = 1; j <= this.layoutRows; j++) {
            for (int i = 1; i <= this.layoutColumns; i++) {
                LayoutHelper lh = new LayoutHelper();
                lh.setColumnnumber(i);
                lh.setRownumber((char)(64 + j));
                output.add(lh);
            }
        }
        return output;
    }
    
    public void setSeatArea(String [][] areaSeatRow, Integer [][] areaSeatColumn, boolean [][] areaSeatOccupied, boolean [][] areaSeatEmpty){
        
    }
    
    public List<Integer> getAllColumns(Long areaid){
        Seats[][] temp = etbr.getSeats(areaid);
        List<Integer> output = new ArrayList<Integer>();
        for(int j = 0; j < temp[0].length; j++){
            output.add(temp[0][j].getColumn());
        }
        return output;
    }
    
    public List<String> getAllRows(Long areaid){
        Seats[][] temp = etbr.getSeats(areaid);
        List<String> output = new ArrayList<String>();
        for(int i = 0; i < temp.length; i++){
            output.add(temp[i][0].getRow());
        }
        return output;
    }
    
    public List<rowHelper> getAllSeats(Long areaid){
        Seats[][] temp = etbr.getSeats(areaid);
        List<rowHelper> output = new ArrayList<rowHelper>();
        for(int i = 0; i < temp.length; i++){
            List<columnHelper> colList = new ArrayList<columnHelper>();
            for(int j = 0; j < temp[i].length; j++){
                columnHelper seat = new columnHelper();
                seat.setColumn( + (temp[i][j].getColumn()));
                if(temp[i][j].isEmpty()){
                    seat.setImagePath("ticketingsymbols/EmptySpaceSeat.gif");
                }else{ 
                    if(temp[i][j].isReserved()){
                        seat.setImagePath("ticketingsymbols/OccupiedSeat.gif");
                    }else{
                        seat.setImagePath("ticketingsymbols/AvailableSeat.gif");
                    }
                }
                colList.add(seat);
            }
            rowHelper row = new rowHelper(String.valueOf(temp[i][0].getRow()), colList);
            output.add(row);
        }
        return output;
    }
    
    public String getServletControlURL(){
        String host = portalManageSB.getHostName() + "AddNewLayoutController";
        return host;
    }
    
}
